package com.example.smartcity.ui.dashboard;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.HistoryRECAdapter;
import com.example.smartcity.ui.Bean.StopHistoryBean;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class StopHistory extends AppCompatActivity implements View.OnClickListener {

    private TextView TVInDate;
    private TextView TVInTime;
    private TextView TVOutDate;
    private TextView TVOutTime;
    private ImageView TVSearch;
    private TextView TV_SeeMore;
    private RecyclerView RCV_history;
    List<StopHistoryBean.RowsBean> list = null;
    private int index = 3;
    private Gson gson;
    private HistoryRECAdapter adapter = null;
    private StopHistoryBean bean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stop_history);
        TVInDate = findViewById(R.id.TV_inDate);
        TVInTime = findViewById(R.id.TV_inTime);
        TVOutDate = findViewById(R.id.TV_outDate);
        TVOutTime = findViewById(R.id.TV_OutTime);
        TVSearch = findViewById(R.id.IV_SearchLast);
        RCV_history = findViewById(R.id.RCV_history);
        TV_SeeMore = findViewById(R.id.TV_SeeMore);
        initView();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        TVInDate.setOnClickListener(this);
        TVInTime.setOnClickListener(this);
        TVOutDate.setOnClickListener(this);
        TVOutTime.setOnClickListener(this);
        TVSearch.setOnClickListener(this);
        OkHttpUtils.getCall(OkHttpUtils.getBaseUrl(getBaseContext())+"/userinfo/parkrecord/list?pageNum=1").enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("TAG", "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String Json = response.body().string();
                gson = new Gson();
                bean = gson.fromJson(Json,StopHistoryBean.class);
                if (bean.getCode()==200){
                    list = bean.getRows();
                    RCV_history.post(new Runnable() {
                        @Override
                        public void run() {
                            LinearLayoutManager manager = new LinearLayoutManager(getBaseContext());
                            manager.setOrientation(RecyclerView.VERTICAL);
                            RCV_history.setLayoutManager(manager);
                            adapter = new HistoryRECAdapter(getBaseContext(),R.layout.historyitemlayout,list.subList(0,index));
                            RCV_history.setAdapter(adapter);
                        }
                    });
                }
            }
        });
        TV_SeeMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (index<=bean.getTotal()-3){
                    index+=3;
                    adapter = new HistoryRECAdapter(getBaseContext(),R.layout.historyitemlayout,list.subList(0,index));
                    RCV_history.setAdapter(adapter);
                }else{
                    Toast.makeText(StopHistory.this, "我也是有底线的", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        Calendar calendar = Calendar.getInstance();
        switch (v.getId()){
            case R.id.TV_inDate:
                setDate(TVInDate,calendar);
                break;
            case R.id.TV_inTime:
                setTime(TVInTime,calendar);
                break;
            case R.id.TV_outDate:
                setDate(TVOutDate,calendar);
                break;
            case R.id.TV_OutTime:
                setTime(TVOutTime,calendar);
                break;
            case  R.id.IV_SearchLast:
                find();
                Log.d("TAG", "onClick: "+"asd");
                break;
        }
    }
    //设置日期
    public void setDate(TextView textView,Calendar calendar){
        new DatePickerDialog(StopHistory.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                textView.setText(year+"-"+month+"-"+dayOfMonth);
            }
        },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
    }
    //设置时间
    public void setTime(TextView textView,Calendar calendar){
        new TimePickerDialog(StopHistory.this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                textView.setText(hourOfDay+":"+minute);
            }
        },calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),false).show();
    }
    public void find(){
                Log.d("TAG", "onClick: "+"ad");
                String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/userinfo/parkrecord/list?pageNum=1&pageSize=10&entryTime="+TVInDate.getText()+" "+TVInTime.getText()+":00&outTime="+TVOutDate.getText()+TVOutTime.getText()+":00";
                OkHttpUtils.getCall(newUrl).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("TAG", "onFailure: "+e.getMessage());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String Json = response.body().string();
                        bean = null;
                        list = null;
                        bean = gson.fromJson(Json,StopHistoryBean.class);
                        if (bean.getCode()==200){
                            list = bean.getRows();
                            adapter = new HistoryRECAdapter(getBaseContext(),R.layout.historyitemlayout,list.subList(0,index));
                            RCV_history.post(new Runnable() {
                                @Override
                                public void run() {
                                    RCV_history.setAdapter(adapter);
                                }
                            });
                        }
                    }
                });
            }
}